<?php

// 
// this takes one of the new-fangled alphanumeric+ embed ids and returns the JSON object for that item
// The ID is extracted by reading the headers of the flash file, then sucking out the Posts id from the returned location header
//
// Sample call:  http://joemaller.com/services/blip_id_extractor.php?blip=Aaf+BQ&callback=?   (note that jQuery should add the callback)
// 

# load the playback page with the new id
$headers = get_headers('http://blip.tv/play/' . htmlspecialchars($_GET['blip']), 1);

# extract the post id from the returned headers
// echo preg_match('%blip.tv/rss/flash/([0-9]+)&%', $headers['Location'], $url);
switch (true) {
    case preg_match('%blip.tv/rss/flash/([0-9]+)&%', $headers['Location'], $url): // standard posts url
        $url = "http://blip.tv/posts/$url[1]?skin=json&file_type=m4v&callback=" . htmlspecialchars($_GET['callback']);
        break;
    
    case preg_match('%blip.tv/bookmarks/rss/([0-9]+)&%', $headers['Location'], $url):   // bookmarks workaround
        $url = "http://pipes.yahoo.com/joemaller/bliptv_bookmarks_handler?_render=json&blip=$url[1]&_callback=" . htmlspecialchars($_GET['callback']);
        break;

    default:
        $url = null;
        break;
}
// echo '/*';
// print_r($url);
// echo '*/';

# redirect to the returned location
header("Location: $url"); 


?>